/*******************************************************************************
 * eAula : a Distributed File Sharing Room on top of a p2p Overlay Network
 * Copyright (C) 2008-2009 Josep Canyelles
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ******************************************************************************/
 
package org.jma.app.ewisdom.entities;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;

import org.jma.lib.soap.utils.XMLSerializable;
import org.jma.lib.utils.strings.StringFormat;

import com.thoughtworks.xstream.XStream;
/**
 * User class represents a User, and stores its properties, userName, password, 
 * name, email address and a Vector of groups that it's member.<br>
 * @author jesmari@ono.com
 * @version 1.0
 * @since 1.0
 *  */
public class User implements Cloneable, Serializable, XMLSerializable {
    private String userName;
    private String password;
    private String name;
    private String emailAddress;
    
    private Vector groups = new Vector();
	
    /**
     * Returns the userName
     * @return userName
     */
	public String getUserName() {
        return userName;
    }

	/**
	 * sets the userName
	 * @param username
	 */
    public void setUserName(String username) {
        this.userName = username;
    }

    /**
     * gets the password string in plain text
     * @return password
     */
    public String getPassword() {
        return password;
    }
    /**
     * gets the password string
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Gets the long name
     * @return long name
     */
    public String getName() {
        return name;
    }

    /**
     * sets the name 
     * @param name long name of a User
     */
    public void setName(String name) {
        this.name = name;
    }


    /**
     * gets the email address
     * @return an email address
     */
	public String getEmailAddress() {
		return emailAddress;
	}

	/**
	 * sets the email address
	 * @param emailAddress an email address 
	 */
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
    public User clone () {
        try {
            return (User)super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }

    }

	/**
	 * Overrides the equals method from Object class. Two Users will be equals if
	 * their userNames are equal.
	 */  
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        User otherUser = (User) o;

        if (userName != null && otherUser.userName!=null)
        	if(userName.equals(otherUser.userName)) return true; 
        	else return false;
        return false;
    }
    
    /**
     * join the user to a group, it only stores the groupName inside a Vector 
     * to avoid crossed references with Group, you must to join the user to a group 
     * and then add the user to this group.<br>
     * @param groupName
     */
    public void joinToGroup(String groupName){
    	groups.add(new String(groupName));
    }
    
    /**
     * Gets the groups Vector
     * @return a Vector with all the user groups 
     */
    public Vector getGroups(){
    	return this.groups;
    }
    
	/**
	 * HashCode calculates its Hashcode
	 * @return Hashcode
	 */   
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (userName != null ? userName.hashCode() : 0);
        return result;
    }

	/**
	 * @see XMLSerializable
	 */
    public String serializeToXML() {
    	XStream xstream = new XStream();
    	return xstream.toXML(this);
    }

}
